package com.hibase.core.oss.util;

import cn.hutool.core.util.StrUtil;
import com.hibase.core.oss.entity.vo.FileUploadVO;
import com.hibase.core.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 文件上传
 *
 * @author hufeng
 * @date 2019/03/22
 */
@Component
public class FileUploadCache {

    public static final String FILE_UPLOAD_KEY = "hibase:upload";

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 添加文件对象到缓存
     */
    public void addFileToCache(String uuid, FileUploadVO tmsFileUploadVO) {
        redisUtil.hPut(FILE_UPLOAD_KEY,uuid,tmsFileUploadVO);
//        RMap<Object, Object> map = redissonClient.getMap(FILE_UPLOAD_KEY);
//        map.put(uuid, tmsFileUploadVO);
        redisUtil.expire(FILE_UPLOAD_KEY, 1, TimeUnit.HOURS);
        // 设置1个小时过期
        //redissonClient.getKeys().expire(FILE_UPLOAD_KEY, 1, TimeUnit.HOURS);
    }

    /**
     * 获取文件缓存
     */
    public FileUploadVO getFileToCache(String uuid) {

        //RMap<Object, Object> map = redissonClient.getMap(FILE_UPLOAD_KEY);
        if (StrUtil.isBlank(uuid)) {
            return null;
        }
        Object result = redisUtil.hGet(FILE_UPLOAD_KEY, uuid);
        if (result != null && result instanceof FileUploadVO) {
            return (FileUploadVO) result;
        }else {
            return null;
        }
    }
}
